<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        :status-icon="true"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            v-model.trim="ruleForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model.trim="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            type="password"
            placeholder="请再次确认密码"
            v-model.trim="ruleForm.repassword"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-row>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            class="btn-login"
            >注册</el-button
          >
        </el-row>
      </el-form>
       <el-link class="loginLink" @click="$router.push('/login')">去登陆</el-link>
      <!-- <router-link class="loginLink" to="/login">去登陆</router-link> -->
    </div>
  </div>
</template>

<script>
export default {
  name: 'reg',
  namespaced: true,
  create () {},
  data () {
    // 注意：必须使用箭头函数
    var validatePass = (rule, value, callback) => {
      // rule:规则对象
      // val对应当前校验规则对应的数据项：repassword
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.repassword !== '') {
          this.$refs.ruleForm.validateField('repassword')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        password: '',
        repassword: '',
        username: ''
      },
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{6,12}$/,
            message: '密码长为6-12位',
            trigger: 'blur'
          },
          // min: 最少输入 3 个字符
          // max: 最多输入 5 个字符
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          // validator用于逻辑判断，此处用于两次密码结果比对
          { validator: validatePass, trigger: 'blur' }
        ],
        repassword: [{ validator: validatePass2, trigger: 'blur' }],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z]{1}[a-zA-Z0-9]{1,9}$/,
            message: '用户名是1-10位字母和数字,且不能以数字开头',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      // validate对整个表单进行校验
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 参数一：url，参数二:要传递的数据对象
          console.log(this.ruleForm)
          const res = await this.$http.post('/api/reg', this.ruleForm)
          console.log(res)
          if (res.data.code === 1) {
            alert(res.data.message)
          } else {
            this.$router.push('/login')
            alert('注册成功')
          }
          // this.$refs[formName].resetFields()
        } else {
          console.log('注册失败!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.reg-container {
  background: url("../../assets/images/login_bg.jpg") center;
  background-size: cover;
  height: 100%;
  position: relative;
  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title-box {
      height: 60px;
      background: url("../../assets/images/login_title.png") center no-repeat;
    }
  }
  .demo-ruleForm {
    font-size: 12px;
    width: 340px;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    .btn-login {
      width: 100%;
    }
  }

  .loginLink {
    font-size: 12px;
    color: #a6a9ad;
    text-decoration: none;

    position: absolute;
    bottom: 20px;
    left: 30px;
  }
}
</style>
